package com.hilda.twitter.timeline.io;

import twitter4j.ResponseList;
import twitter4j.Status;
import android.content.Context;
import android.os.AsyncTask;

import com.hilda.twitter.timeline.twitter.TwitterApp;
import com.hilda.twitter.timeline.utils.Log;

public class TimelineTask extends AsyncTask<Void, Void, ResponseList<Status>> {

	private boolean finished;
	private TwitterApp mTwitterApp;
	private TaskListener mTaskListener;

	public TimelineTask(Context context, TaskListener listener) {
		finished = false;
		mTwitterApp = new TwitterApp(context);
		mTaskListener = listener;
	}

	@Override
	protected ResponseList<twitter4j.Status> doInBackground(Void... params) {
		ResponseList<twitter4j.Status> timeline = null;

		try {
			timeline = mTwitterApp.getHomeTimeline();

		} catch (Exception e) {
			Log.e(this.getClass().toString() + " Error getting timeline", e);
		}

		return timeline;
	}

	@Override
	protected void onPostExecute(ResponseList<twitter4j.Status> result) {

		// if (result != null) {
		// TimelineAdapter adapter = new TimelineAdapter(getActivity(),
		// result);
		// setListAdapter(adapter);
		// }

		finished = true;

		if (mTaskListener != null) {
			mTaskListener.onTaskFinished(result);
		}

		super.onPostExecute(result);
	}

	public boolean isFinished() {
		return finished;
	}

}
